<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - glTF loader</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<meta http-equiv="Access-Control-Allow-Origin" content="*" />
		<style>
			body {
				font-family: Monospace;
				background-color: #000;
				color: #fff;
				margin: 0px;
				overflow: hidden;
			}
			#info {
				color: #fff;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				z-index: 100;
				display:block;
			}
			#info a {
				color: #75ddc1;
				font-weight: bold;
			}
		</style>
	</head>

	<body>
		<div id="info">
			<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - GLTFLoader<br />
			Battle Damaged Sci-fi Helmet by
			<a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
		</div>

		<script src="https://threejs.org/build/three.js"></script>

		<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
		<script src="https://threejs.org/examples/js/loaders/GLTFLoader.js"></script>

		<script src="https://threejs.org/examples/js/Detector.js"></script>
		<script src="https://threejs.org/examples/js/libs/stats.min.js"></script>
		<script src="../js/LegacyGLTFLoader.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats, controls;
			var camera, scene, renderer, light;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 );
				//camera = new THREE.PerspectiveCamera(45,canvas.width/canvas.height,1,4000);
				camera.position.set( 300, 0, 0 );

				controls = new THREE.OrbitControls( camera );
				controls.target.set( 0, -0.2, -0.2 );
				controls.update();

				// envmap
				var path = 'https://threejs.org/examples/textures/cube/skyboxsun25deg/';
				var format = '.jpg';
				var envmap = null;
				try{
					envMap = new THREE.CubeTextureLoader().load( [
					path + 'px' + format, path + 'nx' + format,
					path + 'py' + format, path + 'ny' + format,
					path + 'pz' + format, path + 'nz' + format
				] );
				}catch(e){
					console.log(e.toString());
				}
			

				scene = new THREE.Scene();
				scene.background = envMap;

				light = new THREE.HemisphereLight( 0xbbbbff, 0x444422 );
				light.position.set( 0, 0, 0 );
				scene.add( light );

				light = new THREE.DirectionalLight( 0xffffff );
				light.position.set( 0, 0, 0 );
				scene.add( light );

				// model
				//var loader = new THREE.LegacyGLTFLoader();
				var loader = new THREE.GLTFLoader();
				var loadUrl = "../babylon/ResidentialBuildings001.gltf";
				var loadUrl1 ="../babylon/CamelliaCity.gltf";
				loader.load(loadUrl, function ( gltf ) {

					gltf.scene.traverse( function ( child ) {

						if ( child.isMesh ) {

							child.material.envMap = envMap;

						}

					} );

					scene.add( gltf.scene );

				},undefined,function(e){
					console.log(e.toString());
				});

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.gammaOutput = true;
				container.appendChild( renderer.domElement );

				window.addEventListener( 'resize', onWindowResize, false );

				// stats
				stats = new Stats();
				container.appendChild( stats.dom );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function animate() {

				requestAnimationFrame(animate);

				renderer.render( scene, camera );

				stats.update();

			}
			//window.addEventListener("reset",)

		</script>

	</body>
</html>
